package com.zyp.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName MyFilter
 * @Deserption TODO
 * @Author yinpeng.zhang@ixiandou.com
 * @Date 2019/8/13 11:35
 * @Version 1.0
 **/
@Component
public class MyFilter extends ZuulFilter {

    private static Logger log = LoggerFactory.getLogger(MyFilter.class);
    
    /**
     *
     *@Title:       
     *@Description: 返回一个字符串代表过滤器的类型
     * filterType：返回一个字符串代表过滤器的类型，在zuul中定义了四种不同生命周期的过滤器类型，具体如下：
     * pre：路由之前
     * routing：路由之时
     * post： 路由之后
     * error：发送错误调用
     *@Author: yinpeng.zhang@ixiandou.com
     *@Date:   2019/8/13 11:37
     *
     **/
    @Override
    public String filterType() {
        return "pre";
    }
    
    /**
     *
     *@Title:       
     *@Description: 过滤的顺序
     *
     *@Author: yinpeng.zhang@ixiandou.com
     *@Date:   2019/8/13 11:37
     *
     **/
    @Override
    public int filterOrder() {
        return 0;
    }

    /**
     *
     *@Title:
     *@Description: 这里可以写逻辑判断，是否要过滤，本文true,永远过滤。
     *@param:
     *@return:
     *@Author: yinpeng.zhang@ixiandou.com
     *@Date:   2019/8/13 11:38
     *
     **/
    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));
        Object accessToken = request.getParameter("token");
        if(accessToken == null) {
            log.warn("token is empty");
            ctx.setSendZuulResponse(false);
            ctx.setResponseStatusCode(401);
            try {
                ctx.getResponse().getWriter().write("token is empty");
            }catch (Exception e){}

            return null;
        }
        log.info("ok");
        return null;
    }
}
